﻿package com.jfdesgagne.traklib.tracker {
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.DisplayObjectContainer;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.media.Video;

	public class TrakMotion extends AbstractTrak {
		
		public function TrakMotion(_input:Video, _refStage:DisplayObjectContainer):void {
			super(_input, _refStage);
		}

		/*
		override public function track() : void {
			this.__nowFrame.draw(this.__input, this.__matrix);
			this.__nowFrame.draw(this.__oldFrame, null, null, BlendMode.DIFFERENCE);
			
			super.applyFilters();
			
			this.__nowFrame.threshold( this.__nowFrame, this.__nowFrame.rect, new Point(), '>', 0xFF333333, 0xFFFFFFFF );
			this.__oldFrame.draw( this.__input, this.__matrix );
			
			super.track();
		}
		 */
		 
		 override public function track() : void {
		 	if(this.changed) {
		 		if(this.__multiTracking) {
			 		this.__nowFrame.draw(this.__oldFrame);
					this.__nowFrame.draw(this.__input, this.__matrix, new ColorTransform(), BlendMode.DIFFERENCE);	
					this.__oldFrame.draw(this.__input, this.__matrix);
					
					this.__nowFrame.applyFilter(this.__nowFrame, this.__nowFrame.rect, new Point(), this.__grayCm);
			
					this.__nowFrame.threshold(this.__nowFrame, this.__nowFrame.rect, new Point(), "<=", this.__threshold, 0xff000000);
					this.__nowFrame.threshold(this.__nowFrame, this.__nowFrame.rect, new Point(), "!=", 0xFF000000, 0xFFFFFFFF);
			
					var bmp_noise:BitmapData = this.__nowFrame.clone();
						bmp_noise.applyFilter(bmp_noise, this.__nowFrame.rect, new Point(), this.__noiseReduction);
					this.__nowFrame.threshold(bmp_noise, this.__nowFrame.rect, new Point(), "==", 0xff000000, 0xff000000, 0xFF);
		 		} else {
		 			this.__nowFrame.draw(this.__input, this.__matrix);
					this.__nowFrame.draw(this.__oldFrame, null, null, BlendMode.DIFFERENCE);
					
					super.applyFilters();
					
					this.__nowFrame.threshold( this.__nowFrame, this.__nowFrame.rect, new Point(), '>', 0xFF333333, 0xFFFFFFFF );
					this.__oldFrame.draw( this.__input, this.__matrix );
		 		}
		 		
		 		super.track();
		 	}
		 }
	}
	
}
